package com.yinhai.asynctask;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.littlec.sdk.entity.CMMember;
import com.littlec.sdk.manager.CMGroupManager;
import com.littlec.sdk.utils.CMChatListener;
import com.yinhai.model.IMUserGroup;
import com.yinhai.service.IMUserGroupServiceImpl;

import android.content.Context;
import android.content.Intent;

public class GetUserGroupListTask  extends AsyncTaskForHttp{
	String retStr = null;
	private Context context;
	private IMUserGroupServiceImpl imUserGroupService;
	private String groupId;
	public GetUserGroupListTask(Context context,String groupId) {
		super(context,false);
		this.context = context;
		imUserGroupService = new IMUserGroupServiceImpl(context);
		this.groupId = groupId;
	}
	
//	public GetUserGroupListTask(Context context, boolean autoForword) {
//		super(context, autoForword);
//	}

//	public GetUserGroupListTask(Context context, int timeoutRequestCount,
//			boolean autoForword) {
//		super(context, timeoutRequestCount, autoForword);
//	}
	@Override
	protected void parse(Map<String, Object> result) {
		this.retStr = (String)result.get(AsyncTaskContext.RESPONSE_CONTENT);
		JSONObject jo;
		try {
			jo = new JSONObject(retStr);
			JSONObject jo1 = null;
			if("0".equals(jo.getString("status"))) {
				List<CMMember> members = new ArrayList<CMMember>();
				JSONArray ja = jo.getJSONArray("dataList");
				imUserGroupService.deleteUserGroup(groupId);
				IMUserGroup userGroup = null;
				for(int i = 0; i < ja.length(); i++) {
					userGroup = new IMUserGroup();
					jo1 = ja.getJSONObject(i);
					userGroup.setGroupId(jo1.getString("groupId"));
					userGroup.setUserId(jo1.getString("phone"));
					userGroup.setUserName(jo1.getString("name"));
					imUserGroupService.insert(userGroup);
					members.add(new CMMember(userGroup.getUserId(),userGroup.getUserName()));
				}
				//刷新群组用户列表
				CMGroupManager.getInstance().groupMembers.remove(groupId);
			} 
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
	}
}
